package com.zsy.controller;

import com.zsy.pojo.Result;
import com.zsy.pojo.User;
import com.zsy.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.regex.Pattern;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
//    //我需要一个加密器，Spring 帮我找一个配置好的 BCryptPasswordEncoder 实例来用
//    @Autowired
//    private BCryptPasswordEncoder passwordEncoder;

    @PostMapping("/register")
    public Result register(String username, String password, String email) {

        String usernamePattern = "^[a-zA-Z0-9]{3,12}$";
        String passwordPattern = "^[a-zA-Z0-9]{6,12}$";
        String emailRegex = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}$";

        if (!Pattern.matches(usernamePattern, username)) {
            return Result.error("用户名格式错误，应为3到12个由字符或数字组成的字符串");
        }

        if (!Pattern.matches(passwordPattern, password)) {
            return Result.error("密码格式错误，应为6到12个由字符或数字组成的字符串");
        }

        if (!Pattern.matches(emailRegex, email)) {
            return Result.error("邮箱格式错误！");
        }

        User u = userService.findByUserName(username);
        if (u == null) {
            //加密密码
//            String EncodedPassword = passwordEncoder.encode(u.getPassword());
            userService.register(username, password, email);
            return Result.success();
        } else {
            return Result.error("用户名已经被占用");
        }
    }

    @PostMapping("/login")
    public Result<String> login(String username, String password, HttpServletRequest request) {
        String usernamePattern = "^[a-zA-Z0-9]{3,12}$";
        String passwordPattern = "^[a-zA-Z0-9]{6,12}$";

        //判断用户密码的合法性
        if (!Pattern.matches(usernamePattern, username)) {
            return Result.error("用户名格式错误，应为3到12个由字符或数字组成的字符串");
        }

        // 验证密码格式
        if (!Pattern.matches(passwordPattern, password)) {
            return Result.error("密码格式错误，应为6到12个由字符或数字组成的字符串");
        }

        //查询用户
        User loginUser = userService.findByUserName(username);

        //制断该用户是否存在
        if (loginUser == null) {
            return Result.error("用户名或者密码错误!");
        }

        if (loginUser.getUsername().equals(username) && loginUser.getPassword().equals(password)) {
            // 存放到 HttpSession 中
            request.getSession().setAttribute("username", username);
            return Result.success("登录成功");
        }

        return Result.error("用户名或者密码错误!");
    }

    @GetMapping("/userInfo")
    public Result<User> userInfo(HttpServletRequest request) {
        String username = (String) request.getSession().getAttribute("username");
        if (username == null) {
            return Result.error("未获取到用户信息，请先登录");
        }

        User user = userService.findByUserName(username);
        return Result.success(user);
    }

    @PostMapping("/userPwdService")
    public Result userPwdService(String old_pwd, String new_pwd, String re_pwd, HttpServletRequest request) {
        String username = (String) request.getSession().getAttribute("username");
        User user = userService.findByUserName(username);
        if (user.getPassword().equals(old_pwd)) {
            userService.userPwdService(new_pwd, username);
            return Result.success("修改密码成功");
        } else {
            return Result.error("旧密码输入错误！");
        }
    }
}
